<template>
    <div class="minW1060-auto bg-fff mt10">
        <div class="pad20">
            <el-row class="text-left">
                <div class="dlblock-mb20 mr10">
                    <span class="pr5">分组名称</span>
                    <el-cascader
                            v-model="group"
                            :options="groupTree"
                            :props="{value: 'orgId', label: 'orgName', checkStrictly: true,expandTrigger:'hover'}"
                            style="width: 150px;"
                            :show-all-levels="false"
                            clearable
                    >
                    </el-cascader>
                </div>
                <div class="dlblock-mb20 mr10">
                    <span class="pr5">部门名称</span>
                    <el-input class="pr5" v-model.trim="queryData.deptName" placeholder="请输入内容" style="width: 150px"
                              :maxlength="20"  clearable></el-input>
                </div>
                <div class="dlblock-mb20 pr5 pl5">
                    <el-button @click="pages.page = 1; getDeptsFn();" class="el-icon-search ml5"
                               circle></el-button>
                </div>
                <div class="dlblock-mb20 pl10">
                        <div class="dlblock-mb20 pr10 ml20">
                            <el-button type="primary" @click="addDeptFn()">新增</el-button>
                        </div>
                        <!--<div class="dlblock-mb20 pr10">
                            <el-button type="danger" @click="getDepartmentFn">删除</el-button>
                        </div>-->
                </div>
            </el-row>

            <el-table stripe class="my-table" :data="tableData"  :header-row-class-name="'my-table-header'" v-loading="loading" border>
                <el-table-column align="center" prop="id" label="序号" type="index" width="80"></el-table-column>
                <el-table-column align="center" prop="deptName" label="部门名称"></el-table-column>
                <el-table-column align="center" prop="groupName" label="分组名称"></el-table-column>
                <el-table-column align="center" prop="tel" label="办公电话"></el-table-column>
                <el-table-column align="center" prop="peoPleName" label="部门负责人"></el-table-column>
                <el-table-column align="center" prop="address" label="办公地址" ></el-table-column>
                <el-table-column align="center" prop="countPeople" label="人数(名)"></el-table-column>
                <el-table-column align="center" label="操作" fixed="right">
                    <template slot-scope="{row}">
                        <el-button :inline="true" type="text" @click="getDeptFn(row)">编辑</el-button>&nbsp;
                        <el-button type="text" style="color: #f56c6c" @click="deleteDeptFn(row.deptId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="my-pagination">
                <el-pagination background layout="total, prev, pager, next" :total="pages.total"
                               :current-page="pages.page" @current-change="pageChange">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="`编辑部门`" :visible.sync="editDepts" width="1400px" :close-on-click-modal="false" v-dialogDrag>

            <el-row :gutter="30" class="mt20">
                <el-form label-position="left" label-width="100px" :model="department"
                         ref="department">

                    <el-row>
                        <el-col align="left">
                            <h1 style="padding-left:40px">基本信息</h1>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col align="right">
                            <div class="dlblock-mb20 pr10 ml20">
                                <el-button type="primary" @click="updateDepartmentFn('department')">保存</el-button>
                            </div>
                            <div class="dlblock-mb20 ml20" style="padding-right: 80px !important;">
                                <el-button type="danger" @click="resetForm('department')">重置</el-button>
                            </div>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="部门名称" prop="deptName" :rules="required" >
                                <el-input v-model.trim="department.deptName" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="部门拼音" prop="pinyin" :rules="required">
                                <el-input v-model.trim="department.pinyin" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="拼音缩写" prop="abbr" :rules="required">
                                <el-input v-model.trim="department.abbr" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span="6" :offset="1">
                            <el-form-item label="所属分组" prop="group" >
                                <el-cascader
                                        v-model="department.groupId"
                                        :options="groupTree"
                                        :props="{value: 'orgId', label: 'orgName', checkStrictly: true,expandTrigger:'hover'}"
                                        style="width: 249px;"
                                        :show-all-levels="false"
                                        clearable
                                        placeholder="请选择">
                                </el-cascader>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row style="margin-left: 10px">
                        <el-col align="left">
                            <h1 style="padding:30px 0px 50px 40px">联系信息</h1>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="负责人" prop="empId" :rules="required">
                                <el-select v-model="department.empId" filterable placeholder="请选择" clearable
                                           style="width: 245px">
                                    <el-option v-for="(item,index) in epmInfo"
                                               :key="index"
                                               :label="item.name"
                                               :value="item.empId">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="办公电话" prop="tel">
                                <el-input type="text" v-model.trim="department.tel" placeholder="请输入内容" :max="20"
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="办公地址" prop="address">
                                <el-input v-model.trim="department.address" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row style="margin-left: 10px">
                        <el-col align="left">
                            <h1 style="padding:30px 0px 50px 40px ">部门成员</h1>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-form-item prop="empId">
                            <el-transfer v-if="people" style="margin-left: 200px"
                                         v-model="selectedStaff"
                                         :data="epmInfo"
                                         :titles="['可选', '已选']"
                                         filterable
                                         filter-placeholder="请输入要素名称"
                                         :props="{key: 'empId',label: 'name'}">
                            </el-transfer>
                        </el-form-item>
                    </el-row>

                </el-form>

            </el-row>
        </el-dialog>
    </div>
</template>

<script>
    import {
        deleteDept,getGroupTree,getDept,editDept,getDeptEpmInfo,getEpmInfo
    } from '@/api'
    import {} from '@/utils/config'
    import vPinyin from "../../../utils/vue-py";

    export default {
        name: 'User',
        data() {
            return {
                group: '',
                groupTree: [],
                lineList: [],
                workCode: '',
                workList: [],
                date: [],
                checked: false,
                tableData: [],
                loading: false,
                editDepts:false,
                people: false,
                epmInfo: [],
                selectedStaff: [],
                queryData:{
                    groupId:'',
                    deptName: '',
                },
                department: {
                    deptId: '',
                    deptName: '',
                    groupId: '',
                    group: '',
                    groupName: '',
                    tel: '',
                    peoPleName: '',
                    address: '',
                    pinyin: '',
                    abbr: '',
                    disabled: '',
                    empId: '',
                    empIds: [],
                    flags: '',
                },
                pages: {
                    page: 1,
                    total: 0,
                    size: 18
                },
                required: {
                    required: true,
                    message: '此输入框必填',
                    trigger: 'blur'
                },

            }
        },
        created() {

            this.getGroupTreeFn();
            this.department.group = this.department.groupId

        },
        watch: {

            'department.deptName': {
                handler(newValue, oldValue) {
                    if (this.department.flags != 1) {
                        this.department.pinyin = vPinyin.chineseToPinYin(this.department.deptName)
                        let SX = '';
                        for (var i = 0; i < this.department.pinyin.length; i++) {
                            var c = this.department.pinyin.charAt(i);
                            if (/^[A-Z]+$/.test(c)) {
                                SX += c;
                            }
                        }
                        this.department.abbr = SX;
                    }
                }
            },

        },
        methods: {
            getDeptsFn() {
                this.loading=true
                getDept({
                        pageNum: this.pages.page,
                        groupId:this.group.length ? this.group[this.group.length - 1] : '',
                        deptName:this.queryData.deptName,
                    }).then(res => {
                        this.tableData = res.data.list
                        this.pages.total = res.data.count
                    }).finally(this.loading=false)
            },

            deleteDeptFn(data){
                console.log(data)
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteDept({
                        deptId:data
                    }).then(res => {
                        if (res != null && res.code == 200) {
                            this.$message.success("删除成功")
                            this.getDeptsFn()
                        } else {
                            this.$message.info("删除失败")
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addDeptFn(){
                this.editDepts = true
                this.department.deptId ='',
                this.department.deptName ='',
                this.department.pinyin='',
                this.department.abbr='',
                this.department.disabled='',
                this.department.empId='',
                this.department.tel='',
                this.department.address='',
                this.selectedStaff=''
                this.getEpmInfoFn();
            },
            getDeptFn(row){
                console.log(row)
                this.editDepts = true
                this.department = row
                this.getEpmInfoFn();
            },
            //添加或修改部门信息
            updateDepartmentFn(formName) {
                if (this.department.groupId){
                    var groupId = this.department.groupId.length ? this.department.groupId[this.department.groupId.length - 1] : ''
                }
                this.department.groupId = groupId
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        editDept({
                            deptId: this.department.deptId,
                            name: this.department.deptName,
                            pinyin: this.department.pinyin,
                            abbr: this.department.abbr,
                            disabled: this.department.disabled,
                            empId: this.department.empId,
                            tel: this.department.tel,
                            address: this.department.address,
                            empIds: this.selectedStaff,

                            groupId: this.department.groupId

                        }).then(res => {
                            if (res.code == 200) {
                                this.$message.success("保存成功")
                                this.editDepts = false
                                this.getDeptsFn()
                                }
                        }).catch(err => {
                            this.$message.error("保存失败")

                        })
                    } else {
                        return false;
                    }
                });
            },

            //查询人员
            getEpmInfoFn() {
                getDeptEpmInfo({
                    deptId: this.department.deptId
                }).then(res => {
                    if (res.code == 200) {
                        this.selectedStaff = res.data
                    }
                }),
                getEpmInfo({
                    /*groupId:this.department.groupId,
                       deptId:this.department.deptId*/
                }).then(res => {
                    if (res.code == 200) {
                        this.epmInfo = res.data
                        this.people = true
                    }
                })
            },
            getGroupTreeFn(){
                getGroupTree({}).then(res =>{
                    if (res.code==200){
                        this.groupTree =res.data.children
                        console.log(this.groupTree)
                    }
                })
            },
            pageChange(val) {
                this.pages.page = val
                this.getDepartmentFn()
            }
        }
    }
</script>

<style scoped>
    .my-table /deep/ tr, .my-table /deep/ td{
        padding: 0 !important;
        height: 40px;
        line-height: 40px;
    }


</style>
